﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.House.DAL
{
    /// <summary>
    /// 数据访问类House_BusinessSetting。
    /// </summary>
    public class House_BusinessSetting
    {
        public House_BusinessSetting()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid DepartId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from House_BusinessSetting where DepartId=@DepartId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, DepartId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.House.Entity.House_BusinessSetting model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into House_BusinessSetting(");
            strSql.Append("DepartId,ProtectedNum,ViewOwnerNum,IsHideHouse,IsAloneUserCode,LockHouseDay,IsProtectMark,MarkProtectHours,AllowPrivHouse,AllowPubDemand,IsUseBlacklist,BlacklistLevel,CanAddAtCityId,CanAddAtCityName,CustomerDemandNum,OverTimeTraceNum,IsTelRepeat,UseBlockRole,BlockRoleNum)");

            strSql.Append(" values (");
            strSql.Append("@DepartId,@ProtectedNum,@ViewOwnerNum,@IsHideHouse,@IsAloneUserCode,@LockHouseDay,@IsProtectMark,@MarkProtectHours,@AllowPrivHouse,@AllowPubDemand,@IsUseBlacklist,@BlacklistLevel,@CanAddAtCityId,@CanAddAtCityName,@CustomerDemandNum,@OverTimeTraceNum,@IsTelRepeat,@UseBlockRole,@BlockRoleNum)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "ProtectedNum", DbType.Int32, model.ProtectedNum);
            db.AddInParameter(dbCommand, "ViewOwnerNum", DbType.Int32, model.ViewOwnerNum);
            db.AddInParameter(dbCommand, "IsHideHouse", DbType.Boolean, model.IsHideHouse);
            db.AddInParameter(dbCommand, "IsAloneUserCode", DbType.Boolean, model.IsAloneUserCode);
            db.AddInParameter(dbCommand, "LockHouseDay", DbType.Int32, model.LockHouseDay);
            db.AddInParameter(dbCommand, "IsProtectMark", DbType.Boolean, model.IsProtectMark);
            db.AddInParameter(dbCommand, "MarkProtectHours", DbType.Int32, model.MarkProtectHours);
            db.AddInParameter(dbCommand, "AllowPrivHouse", DbType.Boolean, model.AllowPrivHouse);
            db.AddInParameter(dbCommand, "AllowPubDemand", DbType.Boolean, model.AllowPubDemand);
            db.AddInParameter(dbCommand, "IsUseBlacklist", DbType.Boolean, model.IsUseBlacklist);
            db.AddInParameter(dbCommand, "BlacklistLevel", DbType.Int32, model.BlacklistLevel);
            db.AddInParameter(dbCommand, "CanAddAtCityId", DbType.AnsiString, model.CanAddAtCityId);
            db.AddInParameter(dbCommand, "CanAddAtCityName", DbType.AnsiString, model.CanAddAtCityName);
            db.AddInParameter(dbCommand, "CustomerDemandNum", DbType.Int32, model.CustomerDemandNum);
            db.AddInParameter(dbCommand, "OverTimeTraceNum", DbType.Int32, model.OverTimeTraceNum);
            db.AddInParameter(dbCommand, "IsTelRepeat", DbType.Boolean, model.IsTelRepeat);
            db.AddInParameter(dbCommand, "UseBlockRole", DbType.Boolean, model.UseBlockRole);
            db.AddInParameter(dbCommand, "BlockRoleNum", DbType.Int32, model.BlockRoleNum);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.House.Entity.House_BusinessSetting model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update House_BusinessSetting set ");
            strSql.Append("ProtectedNum=@ProtectedNum,");
            strSql.Append("ViewOwnerNum=@ViewOwnerNum,");
            strSql.Append("IsHideHouse=@IsHideHouse,");
            strSql.Append("IsAloneUserCode=@IsAloneUserCode,");
            strSql.Append("LockHouseDay=@LockHouseDay,");
            strSql.Append("IsProtectMark=@IsProtectMark,");
            strSql.Append("MarkProtectHours=@MarkProtectHours,");
            strSql.Append("AllowPrivHouse=@AllowPrivHouse,");
            strSql.Append("AllowPubDemand=@AllowPubDemand,");
            strSql.Append("IsUseBlacklist=@IsUseBlacklist,");
            strSql.Append("BlacklistLevel=@BlacklistLevel,");
            strSql.Append("CanAddAtCityId=@CanAddAtCityId,");
            strSql.Append("CanAddAtCityName=@CanAddAtCityName,");
            strSql.Append("OverTimeTraceNum=@OverTimeTraceNum,");
            strSql.Append("CustomerDemandNum=@CustomerDemandNum,");
            strSql.Append("UseBlockRole=@UseBlockRole,");
            strSql.Append("BlockRoleNum=@BlockRoleNum,");
            strSql.Append("IsTelRepeat=@IsTelRepeat");
            strSql.Append(" where DepartId=@DepartId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "ProtectedNum", DbType.Int32, model.ProtectedNum);
            db.AddInParameter(dbCommand, "ViewOwnerNum", DbType.Int32, model.ViewOwnerNum);
            db.AddInParameter(dbCommand, "IsHideHouse", DbType.Boolean, model.IsHideHouse);
            db.AddInParameter(dbCommand, "IsAloneUserCode", DbType.Boolean, model.IsAloneUserCode);
            db.AddInParameter(dbCommand, "LockHouseDay", DbType.Int32, model.LockHouseDay);
            db.AddInParameter(dbCommand, "IsProtectMark", DbType.Boolean, model.IsProtectMark);
            db.AddInParameter(dbCommand, "MarkProtectHours", DbType.Int32, model.MarkProtectHours);
            db.AddInParameter(dbCommand, "AllowPrivHouse", DbType.Boolean, model.AllowPrivHouse);
            db.AddInParameter(dbCommand, "AllowPubDemand", DbType.Boolean, model.AllowPubDemand);
            db.AddInParameter(dbCommand, "IsUseBlacklist", DbType.Boolean, model.IsUseBlacklist);
            db.AddInParameter(dbCommand, "BlacklistLevel", DbType.Int32, model.BlacklistLevel);
            db.AddInParameter(dbCommand, "CanAddAtCityId", DbType.AnsiString, model.CanAddAtCityId);
            db.AddInParameter(dbCommand, "CanAddAtCityName", DbType.AnsiString, model.CanAddAtCityName);
            db.AddInParameter(dbCommand, "CustomerDemandNum", DbType.Int32, model.CustomerDemandNum);
            db.AddInParameter(dbCommand, "OverTimeTraceNum", DbType.Int32, model.OverTimeTraceNum);
            db.AddInParameter(dbCommand, "IsTelRepeat", DbType.Boolean, model.IsTelRepeat);
            db.AddInParameter(dbCommand, "UseBlockRole", DbType.Boolean, model.UseBlockRole);
            db.AddInParameter(dbCommand, "BlockRoleNum", DbType.Int32, model.BlockRoleNum);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid DepartId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from House_BusinessSetting ");
            strSql.Append(" where DepartId=@DepartId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, DepartId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static  Chenxin.Lib.House.Entity.House_BusinessSetting GetModel(Guid DepartId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DepartId,ProtectedNum,ViewOwnerNum,IsHideHouse,IsAloneUserCode,LockHouseDay,IsProtectMark,MarkProtectHours,AllowPrivHouse,AllowPubDemand,IsUseBlacklist,BlacklistLevel,CanAddAtCityId,CanAddAtCityName,CustomerDemandNum,OverTimeTraceNum,IsTelRepeat,UseBlockRole,BlockRoleNum from House_BusinessSetting ");
            strSql.Append(" where DepartId=@DepartId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, DepartId);
            Chenxin.Lib.House.Entity.House_BusinessSetting model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM House_BusinessSetting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "House_BusinessSetting");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static  List<Chenxin.Lib.House.Entity.House_BusinessSetting > GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DepartId,ProtectedNum,ViewOwnerNum,IsHideHouse,IsAloneUserCode,LockHouseDay,IsProtectMark,MarkProtectHours,AllowPrivHouse,AllowPubDemand,IsUseBlacklist,BlacklistLevel,CanAddAtCityId,CanAddAtCityName,CustomerDemandNum,OverTimeTraceNum,IsTelRepeat,UseBlockRole,BlockRoleNum ");
            strSql.Append(" FROM House_BusinessSetting ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.House.Entity.House_BusinessSetting > list = new List<Chenxin.Lib.House.Entity.House_BusinessSetting >();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static  Chenxin.Lib.House.Entity.House_BusinessSetting ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.House.Entity.House_BusinessSetting model = new Chenxin.Lib.House.Entity.House_BusinessSetting ();
            object ojb;
            ojb = dataReader["DepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DepartId = new Guid(ojb.ToString());
            }
            ojb = dataReader["ProtectedNum"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ProtectedNum = (int)ojb;
            }
            ojb = dataReader["ViewOwnerNum"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ViewOwnerNum = (int)ojb;
            }
            ojb = dataReader["IsHideHouse"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IsHideHouse = (bool)ojb;
            }
            ojb = dataReader["IsAloneUserCode"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IsAloneUserCode = (bool)ojb;
            }
            ojb = dataReader["LockHouseDay"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.LockHouseDay = (int)ojb;
            }
            ojb = dataReader["IsProtectMark"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IsProtectMark = (bool)ojb;
            }
            ojb = dataReader["MarkProtectHours"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.MarkProtectHours = (int)ojb;
            }
            ojb = dataReader["AllowPrivHouse"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AllowPrivHouse = (bool)ojb;
            }
            ojb = dataReader["AllowPubDemand"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AllowPubDemand = (bool)ojb;
            }
            ojb = dataReader["IsUseBlacklist"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IsUseBlacklist = (bool)ojb;
            }
            ojb = dataReader["BlacklistLevel"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BlacklistLevel = (int)ojb;
            }
            model.CanAddAtCityId = dataReader["CanAddAtCityId"].ToString();
            model.CanAddAtCityName = dataReader["CanAddAtCityName"].ToString();
            ojb = dataReader["CustomerDemandNum"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CustomerDemandNum = (int)ojb;
            }
            ojb = dataReader["OverTimeTraceNum"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OverTimeTraceNum = (int)ojb;
            }
            ojb = dataReader["IsTelRepeat"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IsTelRepeat = (bool)ojb;
            }
            ojb = dataReader["UseBlockRole"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UseBlockRole = (bool)ojb;
            }
            ojb = dataReader["BlockRoleNum"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BlockRoleNum = (int)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}

